//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class BofRecord : BiffRecord
    {
        #region RecordType enum

        public enum BofRecordType
        {
            Chart = 0x20,
            Excel4Macro = 0x40,
            VBModule = 6,
            Workbook = 5,
            Worksheet = 0x10,
            Workspace = 0x100
        }

        #endregion

        public BofRecord()
        {
            
        }

        public BofRecord(BofRecordType type)
        {
            this._type = type;
        }

        public BofRecord(BoundSheetRecord boundSheet): this (BofRecordType.Worksheet)
        {
            _sheet = boundSheet;
        }

        private BofRecordType _type;
        private ushort _data1 = 1536;
        private ushort rupBuild = 12714;
        private ushort rupYear = 1997;
        private ushort flags1 = 32969;
        private byte verLowestBiff = 6;
        private byte verLastXLSaved = 6;
        private ushort reserved2;
        private ushort flags2 = 1;
        private BoundSheetRecord _sheet;

        public BofRecordType GroupType
        {
            get { return _type; }
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.BOF; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            Contract.Assert(this.GroupType != BofRecordType.Worksheet || _sheet != null);
            if (_sheet != null && context.SheetToPosition.Count > 0) //\\
            {
                int position = (int) context.Position - 4;
                using (MovedContext c = context.CreateMovedContext (context.SheetToPosition[_sheet]))
                {
                    context.Write(position);
                }
            }
            context.Writer.Write(_data1);
            context.Writer.Write((ushort)_type);
            context.Writer.Write(rupBuild);
            context.Writer.Write(rupYear);
            context.Writer.Write(flags1);
            context.Writer.Write(flags2);   
            context.Writer.Write(verLowestBiff);
            context.Writer.Write(verLastXLSaved);
            context.Writer.Write(reserved2);
        }
        protected override void ReadContent(BiffReaderContext context, int length)
        {
            _length = length;
            _data1 = context.ReadUInt16();
            _type = (BofRecordType) context.ReadUInt16();
            if (this._type == BofRecordType.Worksheet)
                this._sheet = context.Boundsheets.Dequeue();
            rupBuild = context.ReadUInt16();
            rupYear = context.ReadUInt16();
            flags1 = context.ReadUInt16();
            flags2 = context.ReadUInt16();
            verLowestBiff = context.ReadByte();

            verLastXLSaved = context.ReadByte();
            reserved2 = context.ReadUInt16();

        }

    }
}